package clang.control;

import clang.data.Expression;

public class IfElseBlock implements CodeElement {

	private CodeBlock ifBlock = null, elseBlock = null;
	private Expression condition = null;
	
	public IfElseBlock(Expression condition, CodeBlock ifBlock)
	{
		this.condition = condition;
		this.ifBlock = ifBlock;
		this.elseBlock = null;
	}
	
	public IfElseBlock(Expression condition, CodeBlock ifBlock, CodeBlock elseBlock)
	{
		this.condition = condition;
		this.ifBlock = ifBlock;
		this.elseBlock = elseBlock;
	}
	
	@Override
	public void execute() throws Exception {
		if(condition == null || ifBlock == null) throw new Exception("Incomplete If");

		if(condition.calculate().getBool())
		{
			ifBlock.execute();
		}
		else if(elseBlock != null)
		{
			elseBlock.execute();
		}
	}

}
